var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var uid = 0;
var token = '';

Page({
  data: {
    util: util,
    
    tc: 0,
    uid: 0, //当前登录用户Id
    token: '', //post请求时，当前登录用户token
    cataType: '',

    colorSet: '',
    membercard:'',
    password:'',


    imgWH: 60,

    isIphone_x: false,
    nav_bgColor: '#ffffff',
    
  },

  isMine: 0,

  goAtherPage: function(e) {
    var url = e.currentTarget.dataset.link;
    wx.navigateTo({
      url: url
    })
  },

  onLoad: function(options) {
    var that = this;
    console.log(options);
    var tc = app.get_tc();
    this.setData({
      tc: tc,
    });

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          imgWH: (res.windowWidth - 30) / 5 - 12
        });
      }
    })


     wx.getSystemInfo({
      success: function(res) {
        var isIphone_x = (res.model.indexOf('iPhone X') >= 0);
        that.setData({
          isIphone_x: isIphone_x,
          screen_h: res.windowHeight,
        });
      }
    });
    

    this.setData({   
      membercard : options.membercard ? options.membercard : ''
    })
    this.isMine = options.mine ? options.mine : 0;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    var that = this;
    //获取颜色配置
    app.getColorSetData(function(color) {
      that.setData({
        colorSet: color
      })
      that.resetNavColor();
    });
  },
  //设置titlebar 样式
  resetNavColor: function() {
    var bg_color = '#f0f0f0';
    var fg_color = '#000000';
    if (this.data.colorSet) {
      bg_color = this.data.colorSet.titlebg;
      fg_color = this.data.colorSet.titlefont;
    }
    wx.setNavigationBarColor({
      frontColor: fg_color,
      backgroundColor: bg_color
    })
  },


  loginSubmit: function(e) {
    var that = this;
    var kahao = e.detail.value.kahao;
    var password = e.detail.value.password;
    console.log(e);
    
    if (!kahao) {
      app.dialog(that.data.tc == 1 ? '請填寫電話' : '请填写电话', 'loading');
      return;

    }else{
      var privatekey = utilMd5.hexMD5(that.data.uid + "checkpwdloginsp" + that.data.token);
      util.api({
        data: {
          "privatekey": privatekey,
          "ac": "checkpwdloginsp",
          "uid": that.data.uid,
          "uname": kahao,
          "pwd": password,
          "tc": that.data.tc
        },
        method: "POST",
        success: function (responseData) {
          console.log("成功",responseData);
          if(responseData.data.msg == "true"){
            wx.setStorageSync('membercard', kahao);
            wx.setStorageSync('loginuserid', responseData.data.user.uid);
            wx.showLoading({
              title: 'loading',
            });
            setTimeout(function () {
              wx.hideLoading();
              wx.showToast({
                title: '登入成功,請稍候',
                icon: 'none',
                duration: 1000,
                success: function () {
                  if (that.data.mine == 1) {
                    setTimeout(function () {
                      wx.navigateTo({
                        url: '/pages/mine/mine',
                      })
                    }, 1000);
                  } else {
                    setTimeout(function () {
                      wx.navigateBack({
                        delta: 1
                      })
                    }, 1000);
                  }

                }
              })
            }, 1000);
          }else{
            if(responseData.data.code == "1006"){
              wx.showModal({
                title: '提示',
                content: that.data.tc == 1 ? '電話與密碼不匹配' : '电话与密码不匹配',
                showCancel: false,
              })
            }else{
              wx.showModal({
                title: '提示',
                content: responseData.data.msg,
                showCancel: false,
              })
            }
            
          }
          
        }
      });
      
      
    }

  
  },



  //保存formId
  submitFormId: function (e) {
    app.saveFormId(e.detail.formId);
    var path = e.detail.value.nav_path;
    if (path && path.indexOf('navto:') == 0) {
      path = path.slice(6);
    }
    if (path) {
      wx.reLaunch({
        url: path,
      })
    }
  },



  quxiao:function(){
    console.log("quxiao");
    wx.navigateBack({
        delta: 1
      })
  }

})